package org.crazyit.album.service;

import org.hibernate.SessionFactory;
import org.hibernate.query.Query;

import java.util.List;
import java.io.Serializable;

public class BaseDaoHibernate4<T> implements BaseDao<T> {

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public SessionFactory getSessionFactory() {
        return this.sessionFactory;
    }

    public T get(Class<T> entityClazz, Serializable id) {
        return   getSessionFactory().getCurrentSession()
                .get(entityClazz, id);
    }

    public Serializable save(T entity) {
        return getSessionFactory().getCurrentSession()
                .save(entity);
    }

    public void update(T entity) {
        getSessionFactory().getCurrentSession().saveOrUpdate(entity);
    }

    public void delete(T entity) {
        getSessionFactory().getCurrentSession().delete(entity);
    }

    public void delete(Class<T> entityClazz, Serializable id) {
        delete(get(entityClazz, id));
    }

    public List<T> findAll(Class<T> entityClazz) {
        return find("select en from "
                + entityClazz.getSimpleName() + " en");
    }

    public long findCount(Class<T> entityClazz) {
        List l = find("select count(*) from "
                + entityClazz.getSimpleName());

        if (l != null && l.size() == 1) {
            return (Long) l.get(0);
        }
        return 0;
    }


    protected List  find(String hql) {
        return   getSessionFactory().getCurrentSession()
                .createQuery(hql)
                .list();
    }

    protected List<T> find(String hql, Object... params) {

        Query query = getSessionFactory().getCurrentSession()
                .createQuery(hql);

        for (int i = 0, len = params.length; i < len; i++) {
            query.setParameter(i + "", params[i]);
        }
        return (List<T>) query.list();
    }

    protected List findByPage(String hql,
                              int pageNo, int pageSize) {

        return getSessionFactory().getCurrentSession()
                .createQuery(hql)

                .setFirstResult((pageNo - 1) * pageSize)
                .setMaxResults(pageSize)
                .list();
    }

    protected List findByPage(String hql, int pageNo, int pageSize
            , Object... params) {

        Query query = getSessionFactory().getCurrentSession()
                .createQuery(hql);

        for (int i = 0, len = params.length; i < len; i++) {
            query.setParameter(i + "", params[i]);
        }

        return query.setFirstResult((pageNo - 1) * pageSize)
                .setMaxResults(pageSize)
                .list();
    }
}
